package com.easyidea.its.web.action.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.easyidea.its.util.ItsConstants;
import com.easyidea.its.util.SessionUtil;
import com.easyidea.its.util.StringUtil;
import com.easyidea.its.web.dto.SeesionUserDto;
import com.infowarelab.common.log.Log;
import com.infowarelab.common.log.LogFactory;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionSupport;

public class ItsBaseAction extends ActionSupport {

	private static final long serialVersionUID = -6301185598542935812L;

	/**
	 * 
	 */
	protected Log log = LogFactory.getLog(this.getClass());

	private long actionExecTime = 0L;

	/**
	 * 是否从session中获取查询条件的值，true-是
	 */
	protected boolean sessionFlag = false;

	/**
	 * contextPath of the application, default is "tld". Current we don't know
	 * if is sure
	 */
	protected String contextPath;

	/**
	 * static location
	 */
	protected String staticLocation;

	/**
	 * template location
	 */
	protected String templateLocation;

	/**
	 * branding location
	 */
	protected String brandingStaticLocation;

	/**
	 * current page
	 */
	protected int currentPage = 1;

	/**
	 * 每页显示条数
	 */
	protected int pageSize = 10;

	/**
	 * error message, it will be used by error message showing.
	 */
	protected String errorMsg = null;

	/**
	 * 
	 */
	protected String successMsg = null;

	protected String cleanSucMsg = "false";

	protected Integer clientWidth;

	protected Integer clientHeight;

	private String backUrl;

	public void prepare() throws Exception {

	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * Set contextpath
	 * 
	 * @param contextPath
	 *            web contextPath name
	 */
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	/**
	 * 
	 * Get contextpath
	 * 
	 * @return web contextPath name
	 */
	public String getContextPath() {
		if (contextPath == null) {
			contextPath = getContextPath(ServletActionContext.getRequest());
		}
		return contextPath;
	}

	/**
	 * 
	 * @return web contextPath name
	 */
	public String getContextPath(HttpServletRequest request) {
		return request == null ? "" : request.getContextPath();
	}

	/**
	 * @return Returns the staticLocation such as ${contextPath}/static/default.
	 */
	public String getStaticLocation() {
		if (staticLocation == null) {
			staticLocation = new StringBuffer(ItsConstants.STATIC_PATH).append("/").append(ItsConstants.STYLE_DEFAULT).append("/").toString();
		}
		return staticLocation;
	}

	public void setStaticLocation(String staticLocation) {
		this.staticLocation = staticLocation;
	}

	/**
	 * @return Returns the templateLocation such as template.
	 */
	public String getTemplateLocation() {
		if (templateLocation == null) {
			templateLocation = new StringBuffer(ItsConstants.TEMPLATE_PATH).append("/").toString();
		}
		return templateLocation;
	}

	public void setTemplateLocation(String templateLocation) {
		this.templateLocation = templateLocation;
	}

	/**
	 * @return the brandingStaticLocation such as
	 *         ${contextpath}/branding/{sitename}
	 */
	public String getBrandingStaticLocation() {
		if (brandingStaticLocation == null) {
			brandingStaticLocation = new StringBuffer(ItsConstants.BRANDING_STATIC_PATH).append("/").append(ItsConstants.STYLE_DEFAULT).append("/")
					.toString();
		}
		return brandingStaticLocation;
	}

	/**
	 * @param brandingStaticLocation
	 *            the brandingStaticLocation to set
	 */
	public void setBrandingStaticLocation(String brandingStaticLocation) {
		this.brandingStaticLocation = brandingStaticLocation;
	}

	public long getActionExecTime() {
		return (System.currentTimeMillis() - actionExecTime);
	}

	public void setActionExecTime(long actionExecTime) {
		this.actionExecTime = actionExecTime;
	}

	/**
	 * Get login user info
	 * 
	 * @return SeesionUserDto
	 */
	public SeesionUserDto getSessionUser() {
		SeesionUserDto userdto = (SeesionUserDto) SessionUtil.getSessionValue(ItsConstants.SESSION_KEY_USER_INFO);
		if (userdto == null) {
			userdto = new SeesionUserDto();
		}
		return userdto;
	}

	public String subString(String origin, int len) {
		return StringUtil.substring(origin, len);
	}

	public int lenString(String origin) {
		return StringUtil.length(origin);
	}

	public boolean isSessionFlag() {
		return sessionFlag;
	}

	public void setSessionFlag(boolean sessionFlag) {
		this.sessionFlag = sessionFlag;
	}

	public String getBackUrl() {
		return backUrl;
	}

	public void setBackUrl(String backUrl) {
		this.backUrl = backUrl;
	}

	public String getErrorMsg() {
		return errorMsg;
	}

	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}

	public String getCleanSucMsg() {
		return cleanSucMsg;
	}

	public void setCleanSucMsg(String cleanSucMsg) {
		if (cleanSucMsg.equals("true")) {
			removeSessionAttribute(ItsConstants.SUCCESS_MSG);
		}
		this.cleanSucMsg = cleanSucMsg;

	}

	protected HttpSession getSession() {
		return SessionUtil.getSession();
	}

	protected void removeSessionAttribute(String attributeName) {
		if (SessionUtil.getSession().getAttribute(attributeName) != null) {
			SessionUtil.getSession().removeAttribute(attributeName);
		}

	}

	/**
	 * Get httpRequest
	 * 
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public String getSuccessMsg() {
		return successMsg;
	}

	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}

	public Integer getClientWidth() {
		return clientWidth;
	}

	public void setClientWidth(Integer clientWidth) {
		this.clientWidth = clientWidth;
	}

	public Integer getClientHeight() {
		return clientHeight;
	}

	public void setClientHeight(Integer clientHeight) {
		this.clientHeight = clientHeight;
	}

	public String[] getProvices() {
		return ItsConstants.getProvice();
	}
}
